package ui.views.property;

import java.util.Calendar;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ui.tempdao.EgnMarketVariDao;
import ui.util.CalendarUtil;
import ui.util.IStructuredViewer;
import dao.EgnMarketVari;
import dao.EgnMarketVariHis;
import de.ralfebert.rcputils.builder.table.ColumnBuilder;
import de.ralfebert.rcputils.builder.table.TableViewerBuilder;
import de.ralfebert.rcputils.builder.table.format.Formatter;
import de.ralfebert.rcputils.properties.BaseValue;

public class MarketVariableHistorySection extends AbstractPropertySection implements IStructuredViewer{
	private final static Logger logger = LoggerFactory.getLogger(MarketVariableHistorySection.class);
	private Text labelText;
	private EgnMarketVari mv;
	private TableViewer tableViewer;

@Override
    public void createControls(Composite parent,TabbedPropertySheetPage aTabbedPropertySheetPage) {
        super.createControls(parent, aTabbedPropertySheetPage);
//        Composite comp = new SectionTest(parent,SWT.NONE);
//        Composite tb = new Composite(comp, STANDARD_LABEL_WIDTH);

		TableViewerBuilder t = new TableViewerBuilder(parent);

		ColumnBuilder baseDate = t.createColumn("BaseDate");
//		baseDate.bindToProperty("id.baseDate");
		baseDate.bindToValue(new BaseValue<EgnMarketVariHis>() {
			@Override
			public Object get(EgnMarketVariHis mvHis){
				Calendar cal = mvHis.getId().getBaseDate();
				return CalendarUtil.toString(cal);
			}
		});
//		baseDate.setPercentWidth(60);
		baseDate.useAsDefaultSortColumn();
//		baseDate.makeEditable();
		baseDate.build();


		ColumnBuilder mvId = t.createColumn("MV Id");
		mvId.bindToProperty("id.mvId");
//		mvId.makeEditable();
		mvId.build();

		ColumnBuilder maturity = t.createColumn("Maturity");
		maturity.bindToProperty("id.maturityId");
//		maturity.setPercentWidth(60);
//		maturity.makeEditable();
		maturity.build();

		ColumnBuilder intRate = t.createColumn("MV Value");
		intRate.bindToProperty("mvValue");
		intRate.setPercentWidth(60);
		intRate.makeEditable();
		intRate.build();

		t.setInput(null);
		tableViewer = t.getTableViewer();
    }

    //    private ModifyListener listener = new ModifyListener() {
	//
	//        public void modifyText(ModifyEvent arg0) {
	//            ButtonElementProperties properties = (ButtonElementProperties) buttonElement
	//                .getAdapter(IPropertySource.class);
	//            properties.setPropertyValue(ButtonElementProperties.PROPERTY_TEXT,
	//                labelText.getText());
	//        }
	//    };
		@Override
	    public void setInput(IWorkbenchPart part, ISelection selection) {
	        super.setInput(part, selection);
	        Object input = ((IStructuredSelection) selection).getFirstElement();
	        this.mv = (EgnMarketVari) input;
	        logger.debug("Test MV: {},{}", mv.toString(),mv.getMvId());
	        Calendar cal =EgnMarketVariDao.getEgnMarketVairHisLastDate(mv.getMvId());
	        logger.debug("Calendar:{}", cal);
	        tableViewer.setInput(EgnMarketVariDao.getEgnMarketVariHis(mv.getMvId(),cal));
	        
	    	Formatter.setZebraBackground(tableViewer);
	    }

	@Override
    public void refresh() {
//        labelText.removeModifyListener(listener);
//        ButtonElementProperties properties = (ButtonElementProperties) buttonElement
//            .getAdapter(IPropertySource.class);
//        labelText.setText(properties.strText);
//        labelText.addModifyListener(listener);
		
    }
	
	@Override
	public TableViewer getTableViewer(){
		return tableViewer;
	}
	@Override
	public TreeViewer getTreeViewer(){
		return null;
	}
}
